package com.wondertek.mist.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;

public class FileUtils
{
	public static final String PICFILEPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/cmp2aPicTemp/";
	public static final String ZIPFILEPATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/cmp2aZip/";

	// 将bitmap存储到sd卡上的文件,quality = 100
	public static boolean saveBitmap2file(Bitmap bmp, String appId, String fileName)
	{
		File fileDir = new File(PICFILEPATH);
		if (!fileDir.exists())
		{
			fileDir.mkdir();
		}
		File appIdFileDir = new File(PICFILEPATH + appId);
		if (!appIdFileDir.exists())
		{
			appIdFileDir.mkdir();
		}
		// CompressFormat format = Bitmap.CompressFormat.PNG;
		CompressFormat format = CompressFormat.JPEG;
		int quality = 100;
		OutputStream stream = null;
		try
		{
			stream = new FileOutputStream(PICFILEPATH + appId + "/" + fileName + ".png");
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return bmp.compress(format, quality, stream);
	}

	// 将bitmap存储到sd卡上的文件,quality =50
	public static boolean saveBitmap2Smallfile(Bitmap bmp, String appId, String fileName)
	{
		File fileDir = new File(PICFILEPATH);
		if (!fileDir.exists())
		{
			fileDir.mkdir();
		}
		File appIdFileDir = new File(PICFILEPATH + appId);
		if (!appIdFileDir.exists())
		{
			appIdFileDir.mkdir();
		}
		CompressFormat format = CompressFormat.JPEG;
		int quality = 80;
		OutputStream stream = null;
		try
		{
			stream = new FileOutputStream(PICFILEPATH + appId + "/" + fileName + ".png");
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();

		}
		return bmp.compress(format, quality, stream);
	}

	// 压缩多个文件
	public static File zipFiles(File[] srcfile, String zipFileName)
	{

		File fileDir = new File(ZIPFILEPATH);
		if (!fileDir.exists())
		{
			fileDir.mkdir();
		}
		File zipfile = new File(ZIPFILEPATH + zipFileName + ".zip");
		byte[] buf = new byte[1024];
		try
		{
			// ZipOutputStream类：完成文件或文件夹的压缩
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
			for (int i = 0; i < srcfile.length; i++)
			{
				if (srcfile[i] != null)
				{
					FileInputStream in = new FileInputStream(srcfile[i]);
					out.putNextEntry(new ZipEntry(srcfile[i].getName()));
					int len;
					while ((len = in.read(buf)) > 0)
					{
						out.write(buf, 0, len);
					}
					out.closeEntry();
					in.close();
				}
			}
			out.close();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return zipfile;
	}

	/**
	 * 删除某个文件夹下的所有文件夹和文件
	 * 
	 * @param delpath
	 *            String
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @return boolean
	 */
	public static boolean deletefile(String delpath) throws Exception
	{
		try
		{
			File file = new File(delpath);
			// 当且仅当此抽象路径名表示的文件存在且 是一个目录时，返回 true
			if (!file.isDirectory())
			{
				file.delete();
			}
			else if (file.isDirectory())
			{
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++)
				{
					File delfile = new File(delpath + "/" + filelist[i]);
					if (!delfile.isDirectory())
					{
						delfile.delete();
					}
					else if (delfile.isDirectory())
					{
						deletefile(delpath + "/" + filelist[i]);
					}
				}
				file.delete();
			}

		}
		catch (FileNotFoundException e)
		{
			System.out.println("deletefile() Exception:" + e.getMessage());
		}
		return true;
	}

	// 删除指定文件夹下所有文件
	// param path 文件夹完整绝对路径
	public static boolean delAllFile(String path)
	{
		boolean flag = false;
		File file = new File(path);
		if (!file.exists())
		{
			return flag;
		}
		if (!file.isDirectory())
		{
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++)
		{
			if (path.endsWith(File.separator))
			{
				temp = new File(path + tempList[i]);
			}
			else
			{
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile())
			{
				temp.delete();
			}
			if (temp.isDirectory())
			{
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}

	// 删除文件夹
	// param folderPath 文件夹完整绝对路径
	public static void delFolder(String folderPath)
	{
		try
		{
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			myFilePath.delete(); // 删除空文件夹
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	// 获取指定目录下的所有文件
	public static List<File> getAllPicFile(String delpath)
	{
		List<File> allPicDataList = new ArrayList<File>();
		File file = new File(delpath);
		if (file.isDirectory())
		{
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++)
			{
				File picfile = new File(delpath + "/" + filelist[i]);
				allPicDataList.add(picfile);
			}
		}
		return allPicDataList;
	}

	// 压缩指定目录的所有文件
	public static File zipAllPicByPath(String sourcePath, String zipFileName)
	{
		File fileDir = new File(ZIPFILEPATH);
		if (!fileDir.exists())
		{
			fileDir.mkdir();
		}
		File zipfile = new File(ZIPFILEPATH + zipFileName + ".zip");
		byte[] buf = new byte[1024];

		File file = new File(sourcePath);
		if (file.isDirectory())
		{
			String[] filelist = file.list();
			try
			{
				ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
				for (int i = 0; i < filelist.length; i++)
				{
					File picfile = new File(sourcePath + "/" + filelist[i]);
					FileInputStream in = new FileInputStream(picfile);
					out.putNextEntry(new ZipEntry(picfile.getName()));
					int len;
					while ((len = in.read(buf)) > 0)
					{
						out.write(buf, 0, len);
					}
					out.closeEntry();
					in.close();
				}
				out.close();
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return zipfile;
	}

	// 压缩多个文件
	public static File zipFiles(List<File> srcfile, String zipFileName)
	{

		File fileDir = new File(ZIPFILEPATH);
		if (!fileDir.exists())
		{
			fileDir.mkdir();
		}
		File zipfile = new File(ZIPFILEPATH + zipFileName + ".zip");
		byte[] buf = new byte[1024];
		try
		{
			// ZipOutputStream类：完成文件或文件夹的压缩
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
			for (int i = 0; i < srcfile.size(); i++)
			{
				File tempFile = srcfile.get(i);
				if (tempFile != null)
				{
					FileInputStream in = new FileInputStream(tempFile);
					out.putNextEntry(new ZipEntry(tempFile.getName()));
					int len;
					while ((len = in.read(buf)) > 0)
					{
						out.write(buf, 0, len);
					}
					out.closeEntry();
					in.close();
				}
			}
			out.close();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return zipfile;
	}

	// 根据文件名删除某个文件夹下的文件
	public static boolean deletefile(String delpath, String fileName) throws Exception
	{
		try
		{
			File file = new File(delpath);
			// 当且仅当此抽象路径名表示的文件存在且 是一个目录时，返回 true
			if (file.isDirectory())
			{
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++)
				{
					if (filelist[i].equals(fileName))
					{
						File delfile = new File(delpath + "/" + filelist[i]);
						delfile.delete();
					}
				}
			}
		}
		catch (Exception e)
		{
			System.out.println("deletefile() Exception:" + e.getMessage());
		}
		return true;
	}

}
